/*
Content: Generate dataset with treatments based on rolling sums (for Table G.4.)
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


*********************************
***** DAILY EVENTS ************
*********************************
* For each country in the data expand to have a panel month-year for 1990 to 2018
drop if year<1990
tab year
drop if startdate==.
keep ieb_nat_id
bys ieb_nat_id: gen unique = _n
keep if unique == 1 

expand 10950 // (2019-1990+1)*365=10950

* Create variable startdate for all day-month-year combinations from 1990 to 2019
	gen counter = 1 
	bys ieb_nat_id: gen startdate = 10958	// 10958 is Jan1990 in the day-month-year number classification
	bys ieb_nat_id: gen spell = _n - 1 
	bys ieb_nat_id: replace startdate = startdate + spell 
	format startdate %td		
	
* Merge back the original event data keeping only the variables of interest
merge 1:1 startdate ieb_nat_id using "$globalterror/intermediate/allevents_daily_ieb.dta", gen(_merge_terror) keepusing(ieb_nat_id startdate terror nkill_terror year month ) keep(1 3)


	 g month=month(dofm(startdate))
	 g year=year(startdate)
	 g day=day(startdate)


sort ieb_nat_id startdate 

* If in a given date there is no event assign a zero	
		foreach var in  terror    {
		replace `var' = 0 if _merge_terror == 1 // date with no terror
		
	}

	
********************************************************************************	
** TREATMENT: ROLLING SUMS OF TERROR EVENTS BEFORE INTERVIEW
********************************************************************************

cap drop _merge_terror
cap drop unique counter spell
	
sort ieb_nat_id startdate
foreach var in  terror     {
foreach day in 15 30 90 {
	local start=`day'+1
qui: rangestat (sum) cum_`var'_`day'd=`var', interval(startdate,-`start',-1) by(ieb_nat_id)
}
}
	
	
foreach event in  terror     {	
foreach day in 15 30 90 {
qui: g	atleast1`event'_`day'd=cum_`event'_`day'd>=1 
}
}

sort ieb_nat_id startdate
foreach var in  nkill_terror terror      {
foreach day in 15 30 90 {
local start=`day'
qui: rangestat (sum) cum_`var'_`day'd_0=`var', interval(startdate,-`start',0) by(ieb_nat_id)
}
}


foreach event in  nkill_terror terror     {	
foreach day in 15 30 90 {
qui: g	atleast1`event'_`day'd_0=cum_`event'_`day'd_0>=1 
}
}

		

		
********************************************************************************	
**CONTROL: ROLLING SUMS OF TERROR EVENTS OVER LAGGEG PERIODS
********************************************************************************	

* Total number of terrorist attacks by timing before interview
sort ieb_nat_id startdate
foreach var in terror    {
foreach day in 365 {
foreach t in 90 30 15 {
local end = `t' +1
local start= `end' + `day' 
qui: rangestat (sum) cum_`var'_`day'l_`t's=`var', interval(startdate,-`start',-`end') by(ieb_nat_id)
}
}
}
		


compress
save "$globalterror\final/terror_dailydata_ieb_final.dta", replace 
			
			